package com.pxxy.app_homepage.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.pxxy.app_homepage.entity.Information;
import com.pxxy.security512.R;

import java.util.List;


public class FragmentHomeHotRecyclerAdapter extends RecyclerView.Adapter<FragmentHomeHotRecyclerAdapter.HotHolder> {
    private Context context;
    private List<Information> infoList;

    public FragmentHomeHotRecyclerAdapter(Context context,List<Information> infoList){
        this.context = context;
        this.infoList = infoList;
    }

    @NonNull
    @Override
    public HotHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.homepage_fragment_home_hot_recycler_item,viewGroup,false);
        return new HotHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HotHolder hotHolder, int i) {
        if (i <= 2){
            hotHolder.rank.setTextColor(ContextCompat.getColor(context,R.color.fragment_home_hot_rank_3));
        }else {
            hotHolder.rank.setTextColor(ContextCompat.getColor(context,R.color.fragment_home_hot_rank_4));
        }

        hotHolder.rank.setText(i+1+"");

        hotHolder.title.setText(infoList.get(i).getTitle());
        hotHolder.heatTv.setText(infoList.get(i).getHeat());

        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), new RoundedCorners(10))
                .override(180, 140);
        Glide.with(context).load(infoList.get(i).getAddress().get(0)).apply(options).into(hotHolder.image);

    }

    @Override
    public int getItemCount() {
        return infoList.size();
    }

    //ViewHolder缓存类
    class HotHolder extends RecyclerView.ViewHolder {
        private TextView rank,title,heatTv;
        private ImageView heatIv,image;
        private RelativeLayout hotRl;

        public HotHolder(@NonNull View itemView) {
            super(itemView);
            initView(itemView);
            initEvent();
        }

        private void initEvent() {

        }

        private void initView(View itemView) {
            rank = itemView.findViewById(R.id.fragment_home_hot_recycler_rank);
            title = itemView.findViewById(R.id.fragment_home_hot_recycler_title_tv);
            heatTv = itemView.findViewById(R.id.fragment_home_hot_heat_tv);
            heatIv = itemView.findViewById(R.id.fragment_home_hot_heat_iv);
            hotRl = itemView.findViewById(R.id.fragment_home_hot_recycler_rl);
            image = itemView.findViewById(R.id.fragment_home_hot_recycler_iv);
        }
    }
}
